﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace fhe
{
  namespace core
  {
    public interface IUpdatable
    {
      void Update(TimeSpan dt);
    }

    public class Sim : Node
    {
      public bool Shutdown { get; set; }

      public void Run(TimeSpan? minTime = null, TimeSpan? maxTime = null)
      {
        DateTime start = DateTime.Now;
        DateTime last = start;
        while (!maxTime.HasValue || (last - start) < maxTime.Value)
        {
          while (minTime.HasValue && DateTime.Now - last < minTime.Value)
          {
            System.Threading.Thread.Sleep(minTime.Value - (DateTime.Now - last));
          }
          DateTime now = DateTime.Now;
          TimeSpan dt = now - last;
          last = now;
          Publish<IUpdatable>(n => n.Update(dt));
        }
      }
    }
  }
}
